package com.example.reward.response;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

// RewardController.java
public class StudentDto implements Parcelable {
    private Map<String, Integer> scores;
    private int currentRank;

    public StudentDto(Map<String, Integer> scores, int currentRank) {
        this.scores = scores;
        this.currentRank = currentRank;
    }

    protected StudentDto(Parcel in) {
        currentRank = in.readInt();

        int size = in.readInt();
        if (size > 0) {
            scores = new HashMap<>();
            for (int i = 0; i < size; i++) {
                String key = in.readString();
                Integer value = (Integer) in.readValue(Integer.class.getClassLoader());
                scores.put(key, value);
            }
        } else {
            scores = null;
        }
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(currentRank);

        if (scores != null && !scores.isEmpty()) {
            dest.writeInt(scores.size());
            for (Map.Entry<String, Integer> entry : scores.entrySet()) {
                dest.writeString(entry.getKey());
                dest.writeValue(entry.getValue());
            }
        } else {
            dest.writeInt(0);
        }
    }


    public static final Creator<StudentDto> CREATOR = new Creator<StudentDto>() {
        @Override
        public StudentDto createFromParcel(Parcel in) {
            return new StudentDto(in);
        }

        @Override
        public StudentDto[] newArray(int size) {
            return new StudentDto[size];
        }
    };

    public Map<String, Integer> getScores() {
        return scores;
    }

    public void setScores(Map<String, Integer> scores) {
        this.scores = scores;
    }

    public int getCurrentRank() {
        return currentRank;
    }

    public void setCurrentRank(int currentRank) {
        this.currentRank = currentRank;
    }

    @Override
    public int describeContents() {
        return 0;
    }

}

